﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DocumentFormat.OpenXml.Wordprocessing;
using intelliEssay.Core;
using intelliEssay.Core.DocumentStructure;
using intelliEssay.Interop;
using intelliEssay.Interop.ParagraphStyle;

namespace intelliEssay.Checkers.KeywordsChecker
{
    
    public class intelliEssayKeywordsChecker:IEssayChecker
    {

        public void Run(intelliEssay.Core.Task task)
        {
            CheckKeywords(task);
           // throw new NotImplementedException();
        }

        public void Cancel(intelliEssay.Core.Task task)
        {
           // throw new NotImplementedException();
        }

        public string GetDescription(string cultureInfo)
        {
            return "Check the keywords of document";
           // throw new NotImplementedException();
          
        }

        public string GetName(string cultureInfo)
        {
            return "keywords Checkers";
           // throw new NotImplementedException();
        }


        /*******************************/
        public void CheckKeywords(intelliEssay.Core.Task task)
        {
            //Paragraph standardKeywords=intelliEssay.Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(intelliEssay.Core.DocumentStructure.DocumentParts.KeywordInChinese, 0, task);
            //var keywordsChinese= task.documentStructure.KeywordsInChinese;
            //var keywordsEnglish = task.documentStructure.KeywordsInEnglish;
            //检查中文关键字
            CheckChineseKeywords(task);
            //检查英文关键字
            CheckEnglishKeywords(task);
                  
        }
        private void CheckChineseKeywords(intelliEssay.Core.Task task)
        {
            //取标准文件中文关键字
            Paragraph standardChineseKeywords = intelliEssay.Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(intelliEssay.Core.DocumentStructure.DocumentParts.KeywordInChinese, 0, task);
            var standardKeywordsRuns = standardChineseKeywords.Elements<Run>().ToArray();
            int standardCount=0;
            for (; standardCount< standardKeywordsRuns.Count(); ++standardCount)
            { 
                if(standardKeywordsRuns[standardCount].InnerText.StartsWith("关键词"))
                    break;
            }
            //“关键字”三个字的字体，字号
            var standardKeywordLetters = standardKeywordsRuns[standardCount];
            RunInfoEx standardKeywordRunInfoEx = new RunInfoEx(task.TaskObjectRoot, standardKeywordLetters, task.taskSetting.hostApp);
            string  standardKeywordLettersFont = standardKeywordRunInfoEx["runfonts"];
            string stantardKeywordLettersFontSize = standardKeywordRunInfoEx["FontSize"];

            //取待检查文件关键字
            var taskKeywordsChinese = task.documentStructure.KeywordsInChinese;
            //顶格写检查
            if (taskKeywordsChinese.Count() > 0)
            {
                if (0.0 != DocumentAlignmentHelper.QParagraphEffectiveLeftIndentationInMilimeter(taskKeywordsChinese[0], task.TaskObjectRoot, task.taskSetting.paperWidth, true, task.taskSetting.hostApp))
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskKeywordsChinese[0], task, true));
                }



                //第一行
                Paragraph firstLineKeywordParagraph = taskKeywordsChinese[0];
                //检查是否“关键词”开头
                if (!firstLineKeywordParagraph.InnerText.StartsWith("关键词"))
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskKeywordsChinese[0], task, true));
                }
                var taskRuns = firstLineKeywordParagraph.Elements<Run>().ToArray();
                int taskCount = 0;
                for (taskCount = 0; taskCount < taskRuns.Count(); ++taskCount)
                {
                    string text = taskRuns[taskCount].InnerText.TrimStart();
                    if (text.StartsWith("关键词") || text.StartsWith("键词") || text.StartsWith("词"))
                    {
                        break;
                    }
                }
                if (taskCount >= taskRuns.Count())
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", firstLineKeywordParagraph, task, true));
                else
                {
                    for (int j = 0; j < taskCount; ++j)
                    {
                        RunInfoEx taskRunInfoEx = new RunInfoEx(task.TaskObjectRoot, taskRuns[j], task.taskSetting.hostApp);

                        string taskKeywordLettersFont = taskRunInfoEx["runfonts"];
                        string taskKeywordLetterFontSize = taskRunInfoEx["FontSize"];
                        //检查字体
                        if (taskKeywordLettersFont != standardKeywordLettersFont)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                        //检查字号
                        if (taskKeywordLetterFontSize != stantardKeywordLettersFontSize)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                    }
                }
                /****************************************/
                //关键字内容检查

                Paragraph standardCNKeywordsContent = intelliEssay.Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(intelliEssay.Core.DocumentStructure.DocumentParts.KeywordInChineseContent, 0, task);
                var standardCNKeywordsContentRuns = standardCNKeywordsContent.Elements<Run>().ToArray();


                RunInfoEx standardCNKeywordsContentRunInfoEx = new RunInfoEx(task.TaskObjectRoot, standardCNKeywordsContentRuns[0], task.taskSetting.hostApp);
                string standardKeywordContentFont = standardCNKeywordsContentRunInfoEx["runfonts"];
                string standardKeywordContentFontSize = standardCNKeywordsContentRunInfoEx["FontSize"];


                ++taskCount;
                for (int j = taskCount; j < taskRuns.Count(); ++j)
                {
                    RunInfoEx taskKeywordsContentRunInfoEx = new RunInfoEx(task.TaskObjectRoot, taskRuns[j], task.taskSetting.hostApp);
                    string taskKeywordsContentFont = taskKeywordsContentRunInfoEx["runfonts"];
                    string taskKeywordsContentFontSize = taskKeywordsContentRunInfoEx["FontSize"];


                    // Console.WriteLine(taskKeywordsContentFont);
                    //Console.WriteLine(taskKeywordsContentFontSize);
                    if (standardKeywordContentFont != taskKeywordsContentFont)
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                    }
                    if (standardKeywordContentFontSize != taskKeywordsContentFontSize)
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                    }
                }
                Console.WriteLine(standardKeywordContentFont);
                Console.WriteLine(standardKeywordContentFontSize);
                for (int i = 1; i < taskKeywordsChinese.Count(); ++i)
                {
                    Console.WriteLine(taskKeywordsChinese[i].InnerText);
                    var taskKeywordsChineseRuns = taskKeywordsChinese[i].Elements<Run>().ToArray();
                    for (int j = 0; j < taskKeywordsChineseRuns.Count(); ++j)
                    {
                        RunInfoEx taskKeywordsChineseContentRunInfoEx = new RunInfoEx(task.TaskObjectRoot, taskKeywordsChineseRuns[i], task.taskSetting.hostApp);
                        string taskKeywordsContentFont = taskKeywordsChineseContentRunInfoEx["runfonts"];
                        string taskKeywordsContentFontSize = taskKeywordsChineseContentRunInfoEx["FontSize"];

                        Console.WriteLine(taskKeywordsContentFont);
                        Console.WriteLine(taskKeywordsContentFontSize);

                        if (standardKeywordContentFont != taskKeywordsContentFont)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                        if (standardKeywordContentFontSize != taskKeywordsContentFontSize)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                    }
                }
            }


        }
        private void CheckEnglishKeywords(intelliEssay.Core.Task task)
        {
            Paragraph standardEnglishKeywords = intelliEssay.Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(intelliEssay.Core.DocumentStructure.DocumentParts.KeywordInEnglish,0,task);
            var standardKeywordsRuns = standardEnglishKeywords.Elements<Run>().ToArray();
            int standardCount = 0;
            for (; standardCount < standardKeywordsRuns.Count(); ++standardCount)
            {
                if (standardKeywordsRuns[standardCount].InnerText.StartsWith("Keywords"))
                    break;
            }
            //“Keywords”字体，字号
            var standardKeywordLetters = standardKeywordsRuns[standardCount];
            RunInfoEx standardKeywordRunInfoEx = new RunInfoEx(task.TaskObjectRoot, standardKeywordLetters, task.taskSetting.hostApp);
            string standardKeywordLettersFont = standardKeywordRunInfoEx["runfonts"];
            string stantardKeywordLettersFontSize = standardKeywordRunInfoEx["FontSize"];
            bool standardKwywordLettersIsBold = standardKeywordRunInfoEx["isbold"];
             //取待检查文件关键字
            var taskKeywordsEnglish = task.documentStructure.KeywordsInEnglish;

            //顶格写检查
            if (taskKeywordsEnglish.Count() > 0)
            {
                if (0.0 != DocumentAlignmentHelper.QParagraphEffectiveLeftIndentationInMilimeter(taskKeywordsEnglish[0], task.TaskObjectRoot, task.taskSetting.paperWidth, true, task.taskSetting.hostApp))
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskKeywordsEnglish[0], task, true));
                }


                //第一行
                Paragraph firstLineKeywordParagraph = taskKeywordsEnglish[0];
                if (!firstLineKeywordParagraph.InnerText.StartsWith("Keywords"))
                {
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskKeywordsEnglish[0], task, true));
                }

                var taskRuns = firstLineKeywordParagraph.Elements<Run>().ToArray();
                int taskCount = 0;
                for (; taskCount < taskRuns.Count(); ++taskCount)
                {
                    string text = taskRuns[taskCount].InnerText.TrimStart();
                    if (text.StartsWith("Keywords") || text.StartsWith("eywords") || text.StartsWith("ywords") || text.StartsWith("words") || text.StartsWith("ords") || text.StartsWith("rds") || text.StartsWith("rds") || text.StartsWith("ds") || text.StartsWith("s"))
                    {
                        break;
                    }

                }
                if (taskCount >= taskRuns.Count())
                    task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", firstLineKeywordParagraph, task, true));
                else
                {
                    for (int j = 0; j < taskCount; ++j)
                    {
                        RunInfoEx taskRunInfoEx = new RunInfoEx(task.TaskObjectRoot, taskRuns[j], task.taskSetting.hostApp);

                        string taskKeywordLettersFont = taskRunInfoEx["runfonts"];
                        string taskKeywordLetterFontSize = taskRunInfoEx["FontSize"];
                        bool taskKeywordLetterIsBold = taskRunInfoEx["isbold"];
                        //检查字体
                        if (taskKeywordLettersFont != standardKeywordLettersFont)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                        //检查字号
                        if (taskKeywordLetterFontSize != stantardKeywordLettersFontSize)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                        //检查是否加粗
                        if (taskKeywordLetterIsBold != standardKwywordLettersIsBold)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }



                    }
                }
                /****************************************/
                //关键字内容检查

                Paragraph standardENKeywordsContent = intelliEssay.Core.DocumentStructure.StandardFormats.GetStandardFormatByDocumentPart(intelliEssay.Core.DocumentStructure.DocumentParts.KeywordInEnglishContent, 0, task);

                var standardENKeywordsContentRuns = standardENKeywordsContent.Elements<Run>().ToArray();
                RunInfoEx standardKeywordsContentRunInfoEx = new RunInfoEx(task.TaskObjectRoot, standardENKeywordsContentRuns[0], task.taskSetting.hostApp);
                string standardKeywordContentFont = standardKeywordsContentRunInfoEx["runfonts"];
                string standardKeywordContentFontSize = standardKeywordsContentRunInfoEx["FontSize"];


                ++taskCount;
                for (int j = taskCount; j < taskRuns.Count(); ++j)
                {
                    RunInfoEx taskKeywordsContentRunInfoEx = new RunInfoEx(task.TaskObjectRoot, taskRuns[j], task.taskSetting.hostApp);
                    string taskKeywordsContentFont = taskKeywordsContentRunInfoEx["runfonts"];
                    string taskKeywordsContentFontSize = taskKeywordsContentRunInfoEx["FontSize"];
                    if (standardKeywordContentFont != taskKeywordsContentFont)
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                    }
                    if (standardKeywordContentFontSize != taskKeywordsContentFontSize)
                    {
                        task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                    }
                }

                for (int i = 1; i < taskKeywordsEnglish.Count(); ++i)
                {
                    var taskKeywordsEnglishRuns = taskKeywordsEnglish[i].Elements<Run>().ToArray();
                    for (int j = 0; j < taskKeywordsEnglishRuns.Count(); ++j)
                    {
                        RunInfoEx taskKeywordsEnglishContentRunInfoEx = new RunInfoEx(task.TaskObjectRoot, taskKeywordsEnglishRuns[i], task.taskSetting.hostApp);
                        string taskKeywordsContentFont = taskKeywordsEnglishContentRunInfoEx["runfonts"];
                        string taskKeywordsContentFontSize = taskKeywordsEnglishContentRunInfoEx["FontSize"];

                        if (standardKeywordContentFont != taskKeywordsContentFont)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                        if (standardKeywordContentFontSize != taskKeywordsContentFontSize)
                        {
                            task.AddEssayExceptionToEssayExceptionCollection(new EssayException(ExceptionType.Formatting, "关键词格式错误", taskRuns[j], task, true));
                        }
                    }
                }
            }
        }
    }
}
